package com.zxy.controller;


import com.zxy.common.result.Result;
import com.zxy.entity.User;
import com.zxy.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zxy
 * @since 2021-04-21
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/register")
    public Result register(@Validated @RequestBody User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.fail("数据有误，请检查后重新输入");
        }
        return userService.register(user);
    }

    @PostMapping("/login")
    public Result login(@Validated @RequestBody User user, BindingResult bindingResult, HttpServletResponse response) {
        if (bindingResult.hasErrors()) {
            return Result.fail("数据有误，请检查后重新输入");
        }
        return userService.login(user, response);
    }
}
